<template>
	<view class="promotion">
		<y-cubar title="我的推广" :bgColor="`rgba(255,255,255,${opacity})`" />
		<view class="promotion-bg"></view>
		<view class="card">
			<image class="card-bg" src="/static/user/reward-bg.png" mode=""></image>
			<view class="card-info">
				<view class="card-info-left">
					<view class="name">
						<text style="padding-right: 10rpx;">累计佣金(元)</text>
						<view @click="handDesc">
							<uv-icon name="error-circle" color="#333333"></uv-icon>
						</view>
					</view>
					<view class="num">{{ userInfo.brokerage || 0 }}</view>
				</view>
				<button class="cu-btn" @click="handWith">立即提现</button>
			</view>
		</view>
		<view class="con">
			<view class="cell">
				<view class="info">
					<image src="/static/user/reward/qer.png" mode=""></image>
					<text>推广二维码</text>
				</view>
				<button class="cu-btn" @click="handOpen">去推广</button>
			</view>
			<view class="cell">
				<view class="info">
					<image src="/static/user/reward/share.png" mode=""></image>
					<text>分享</text>
				</view>
				<button class="cu-btn" open-type="share">去分享</button>
			</view>
			<view class="cell">
				<view class="info">
					<image src="/static/user/reward/next.png" mode=""></image>
					<text>我的下级</text>
				</view>
				<button class="cu-btn" @click="handNext">去查看</button>
			</view>
		</view>
		<view class="nav-bar">
			<view class="title">佣金明细</view>
			<view class="more" @click="handMore">
				<text>查看更多</text>
				<uv-icon name="arrow-right"></uv-icon>
			</view>
		</view>
		<common-list :isDataEmpty="tableData.length === 0" :status="status">
			<template #list>
				<view>
					<view class="list">
						<view class="item" v-for="(item, index) in tableData" :key="index">
							<view class="info">
								<view class="name">{{ item.type === 'AWARD' ? '租车奖励' : '提现' }}</view>
								<view class="date">{{ item.createTime }}</view>
							</view>
							<view class="num">+{{ item.brokerageMoney }}</view>
						</view>
					</view>
				</view>
			</template>
		</common-list>
		<eqr-pop ref="eqrRef" />
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onPageScroll,
		onShareAppMessage,
		onReachBottom,
		onPullDownRefresh,
	} from "@dcloudio/uni-app";
	import {
		useAppsStore
	} from '@/stores/modules/app.js'
	import {
		storeToRefs
	} from 'pinia'
	import {
		doYjList
	} from '@/apis/user.js'
	import {
		userList
	} from '@/hooks/userList.js'
	import YCubar from '@/components/y-cubar/index.vue'
	import EqrPop from './components/erqPop.vue'
	import CommonList from '@/components/commonList/list.vue'

	const useStore = useAppsStore()
	const {
		getUserInfo: userInfo,
		getCityName: cityName,
	} = storeToRefs(useStore)
	const {
		setPid
	} = useStore

	const opacity = ref(0)
	onPageScroll((e) => {
		const childerOpa = Number((e.scrollTop / 200).toFixed(2))
		opacity.value = childerOpa >= 1 ? 1 : childerOpa
	})

	const eqrRef = ref()
	const handOpen = () => {
		eqrRef.value.handOpen()
	}

	const handMore = () => {
		uni.navigateTo({
			url: '/pagesA/user/priceDetail'
		})
	}

	const handWith = () => {
		uni.navigateTo({
			url: '/pagesA/user/withdrawal?type=BROKERAGE'
		})
	}

	const handDesc = () => {
		uni.navigateTo({
			url: '/pagesA/other/rich?val=yjsm'
		})
	}

	const handNext = () => {
		uni.navigateTo({
			url: '/pagesA/user/childer'
		})
	}

	/**
	 * 
	 * 列表数据
	 * */
	const getApi = async (params) => {
		const res = await doYjList(params)
		return res
	}
	const {
		tableData,
		status,
		fetchData,
		onBottom,
		onRefresh,
		queryForm
	} = userList({
		apis: {
			getApi
		},
		initForm: {}
	})

	onPullDownRefresh(() => {
		onRefresh()
	})
	onReachBottom(() => {
		onBottom()
	})
	onLoad(async (e) => {
		if (e?.pid) {
			setPid(e.pid)
		}
		if (!cityName.value) {
			await getApp().getCity()
		}
		onRefresh()
	})

	onShareAppMessage(() => {
		let path = `pages/tabbar/home/index`
		if (userInfo.value?.id) {
			path += `?pid=${userInfo.value.id}`
		}
		return {
			path,
			imageUrl: 'https://bc-haowanjia.oss-cn-hangzhou.aliyuncs.com/images/169321246649027.png'
		}
	})
</script>

<style lang="scss" scoped>
	.promotion {
		min-height: 100vh;
		background-color: #F5F5F5;
		padding-bottom: 150rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 150rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 150rpx);

		&-bg {
			width: 750rpx;
			height: 534rpx;
			background: linear-gradient(180deg, #FFC434 0%, #FFCE55 29%, #F5F5F5 100%);
			position: absolute;
			top: 0;
		}

		.card {
			position: relative;
			z-index: 3;
			width: 650rpx;
			height: 158rpx;
			position: relative;
			margin: 40rpx auto 0 auto;

			&-bg {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				display: block;
			}

			&-info {
				position: relative;
				height: 100%;
				z-index: 4;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 52rpx;

				&-left {
					.name {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #95673B;
						display: flex;
						align-items: center;
					}

					.num {
						margin-top: 10rpx;
						font-size: 48rpx;
						font-family: PingFang SC-Heavy, PingFang SC;
						font-weight: 800;
						color: #803C00;
					}
				}

				.cu-btn {
					height: 70rpx;
					background: linear-gradient(180deg, #FFBE2F 0%, #FC792D 100%);
					border-radius: 16rpx;
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

		.con {
			width: 702rpx;
			margin: -20rpx auto 0 auto;
			position: relative;
			z-index: 4;
			background-color: #fff;
			border-radius: 20rpx;
			padding: 32rpx 24rpx;

			.cell {
				display: flex;
				align-items: center;
				justify-content: space-between;

				&:not(:first-child) {
					margin-top: 43rpx;
				}

				.info {
					display: flex;
					align-items: center;

					image {
						width: 76rpx;
						height: 76rpx;
					}

					text {
						margin-left: 20rpx;
						font-size: 30rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #333333;
					}
				}

				.cu-btn {
					background-color: rgba(255, 255, 255, 0);
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FF930D;
					border: 1rpx solid #FF930D;
					border-radius: 16rpx;
				}
			}
		}

		.nav-bar {
			margin: 40rpx 24rpx 0 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.title {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.more {
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				display: flex;
				align-items: center;
			}
		}

		.list {
			margin: 20rpx 24rpx;
			background-color: #fff;
			padding: 24rpx;
			box-sizing: border-box;
			border-radius: 16rpx;

			.item {
				padding: 28rpx 0;
				border-bottom: 1rpx solid #EEEEEE;
				display: flex;
				align-items: center;
				justify-content: space-between;

				&:not(:first-child) {
					margin-top: 40rpx;
				}

				.info {
					.name {
						font-size: 30rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
					}

					.date {
						margin-top: 8rpx;
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}
				}

				.num {
					font-size: 32rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}
		}
	}
</style>